/**
 * 异常总类
 */
package cn.antia.cep.common;

import java.util.*;

/**
 * @author bin
 * @version 0.1
 * @date Sep 28, 2006
 */
public class AntiaException extends RuntimeException {
	private static final String EXCEPTION_FILE = "cn.antia.cep.common.exception";
	private static final long serialVersionUID = 0;
	private static final ResourceBundle resourceBundle = ResourceBundle.getBundle(EXCEPTION_FILE);
	private String code;
	private boolean reparable = true; //异常是否可以恢复，默认为可以恢复
	private String errorMsg;
    private String etype = "comm";

    public AntiaException(){
	}
    
    public AntiaException(String code){
        setCode(code);
    }
    public AntiaException(String code,String errorMsg){
        setCode(code);
        this.errorMsg = errorMsg;
    }

    public AntiaException(String code,boolean reparable){
		setCode(code);
		this.reparable = reparable;
		errorMsg = "";
	}
	
	public AntiaException(String code,String errorMsg,boolean reparable){
		setCode(code);
		this.reparable = reparable;
		this.errorMsg = errorMsg;
	}

    protected void setCode(String code){
        this.code = code;
    }
    public String getCode(){
        return code;
    }

    /**
     * 异常错误信息
     * @return msg
     */
    public String getMessage(){
		String message;
		//如果没有设置异常代码，则设为001异常
		if(code == null || code.equals("")){
			code = "001";
		}
		message = new StringBuffer().append("错误代号：").append(code).append(":<br>").append(resourceBundle.getString(code)).toString();
		if(message == null || message.equals("")){
			return new StringBuffer().append("Error Code \"").append(code).append("\" Not Defined!").toString();
		}
		return new StringBuffer().append(message).append("<br>Cause:").append(errorMsg).toString();
	}

    /**
     * 取定义好的错误信息
     * @return 定义好的错误信息
     */
    public String getDefineMsg(){
        if(code == null || code.equals("")){
			code = "001";
		}
        String message = new StringBuffer().append("错误代号：").append(code).append(":<br>").append(resourceBundle.getString(code)).toString();
		if(message == null || message.equals("")){
			return new StringBuffer().append("Error Code \"").append(code).append("\" Not Defined!").toString();
		}
        return message;
    }

    /**
     * 是否可容忍错误
     * 不可容忍则表示系统运行出错(未预期错误)
     * @return bool
     */
    public boolean isReparable() {
        return reparable;
    }
    public String getErrorMsg() {
        return errorMsg;
    }
	public boolean getReparable(){
		return reparable;
	}

    public String getEtype() {
        return etype;
    }

    public void setEtype(String etype) {
        this.etype = etype;
    }
}
